package com.hundsun.cprs.yyt.service.account.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.service.account.dao.AccountQuotaDailyDao;
import com.hundsun.cprs.yyt.service.account.domain.AccountQuotaDaily;
import com.hundsun.cprs.yyt.service.account.domain.query.AccountQuotaDailyQuery;
import com.hundsun.cprs.yyt.service.account.service.AccountQuotaDailyService;

@Service
public class AccountQuotaDailyServiceImpl extends BaseService implements AccountQuotaDailyService {
    
    @Autowired
    private AccountQuotaDailyDao accountQuotaDailyDao;

    @Override
    public void selectByPage(AccountQuotaDailyQuery query) {
       List<AccountQuotaDaily> result = accountQuotaDailyDao.selectByQuery(query);
       query.compatible(result);
    }

    @Override
    public AccountQuotaDaily sumByQuery(AccountQuotaDailyQuery query) {
        query.onlyReturnList();
        AccountQuotaDaily sumResult = accountQuotaDailyDao.sumByQuery(query);
        return sumResult;
    }

    @Override
    @Transactional
    public void statQuotaDaily(Date reportDate) {
        Assert.notNull(reportDate,"报表日期必填");
        Date startDate = DateUtils.truncate(reportDate, Calendar.DAY_OF_MONTH);
        Date endDate = new Date(reportDate.getTime());
        endDate = DateUtils.setHours(endDate, 23);
        endDate = DateUtils.setMinutes(endDate, 59);
        endDate = DateUtils.setSeconds(endDate, 59);
        endDate = DateUtils.setMilliseconds(endDate, 999);
        //先删除已生成的数据
        accountQuotaDailyDao.deleteByReportDate(startDate);
        //再统计报表
        accountQuotaDailyDao.insertFromQuotaLog(startDate, endDate);
    }

}
